package com.validant.enigma3.samples;

import java.util.Iterator;

import org.openqa.selenium.Keys;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.validant.enigma3.common.Constants;
import com.validant.enigma3.common.TestBase;
import com.validant.enigma3.common.Utils;
import com.validant.enigma3.controls.ExtWebElement;

public class SimpleTasks extends TestBase {

	// control names defined in UIMap.xls
	public static final String CTRL_NEW_TASK = "txtNewTask";
	public static final String CTRL_LBL_TASK = "lblTask1";
	public static final String CTRL_TXT_TASK = "txtTask";		
	
	public static final String CTRL_BTN_DELETE = "btnDelete";
	public static final String CTRL_BTN_YES = "btnYes";
	
	// test data
	public static final String TASK_NAME_1 = "New Task Created";
	public static final String TASK_NAME_2 = "Task Updated";
	
	private static final String NEW_TASK_DATA_SHEET = "CreateTask";
	private static final String NEW_TASK_DATA_PROVIDER = "Login Data Provider";
	
	/*
	@Test(description = "Create a new task")
	public void createTask() {
		// enter data
		ExtWebElement ctrlNewTask= new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_NEW_TASK)));
		ctrlNewTask.sendKeys(TASK_NAME_1);
		ctrlNewTask.sendKeys(Keys.ENTER);
		
		// verify data
		ExtWebElement lblTask = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_LBL_TASK)));
		lblTask.verifyTextEquals(TASK_NAME_1);		
	}*/

	@DataProvider(name = NEW_TASK_DATA_PROVIDER)	
	public Iterator<Object[]> createTaskDataProvider(ITestContext context) {
		return Utils.createDataProvider(Utils.getTestParameter(context, Constants.PARAM_TEST_DATA_FILE), NEW_TASK_DATA_SHEET);
	}	
	
	@Test(description = "Create new tasks", dataProvider = NEW_TASK_DATA_PROVIDER)
	public void createTask(String task, String ctrlName) {
		// enter data
		ExtWebElement ctrlNewTask= new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_NEW_TASK)));
		ctrlNewTask.sendKeys(task);
		ctrlNewTask.sendKeys(Keys.ENTER);
		
		// verify data
		ExtWebElement lblTask = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(ctrlName)));
		lblTask.verifyTextEquals(task);		
	}
		
	@Test(description = "Edit an existing task")
	public void editTask() {
		// double click to edit a task
		ExtWebElement lblTask = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_LBL_TASK)));
		driver.doubleClick(lblTask);

		// edit task with new name
		ExtWebElement txtTask = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_TXT_TASK)));
		txtTask.sendKeys(TASK_NAME_2);
		txtTask.sendKeys(Keys.ENTER);
		
		// verify data
		ExtWebElement _lblTask = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_LBL_TASK)));
		_lblTask.verifyTextContains(TASK_NAME_2);
	}
	
	@Test(description = "Delete an existing task")
	public void deleteTask() throws InterruptedException {
		// click to select the task
		ExtWebElement lblTask = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_LBL_TASK)));
		lblTask.click();
		
		// click Delete button
		ExtWebElement btnDelete = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_BTN_DELETE)));
		btnDelete.click();
		
		// Click Yes to delete task
		ExtWebElement btnYes = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_BTN_YES)));
		btnYes.click();
		
		// Verify the task is deleted successfully
		driver.verifyElementNotPresent(uimap.getElement(CTRL_LBL_TASK));
	}
	
}
